package edu.udo.sopra10.chaturaji.gui.util;

/**
 * A GuiLock for synchronization.
 * 
 * @author Patrick Westerhoff
 */
public class GuiLock
{
	private boolean locked = false;
	
	/**
	 * Locks the GuiLock and pauses the current thread until the lock gets
	 * unlocked.
	 * 
	 */
	public synchronized void lockAndWait ()
	{
		locked = true;
		
		while ( locked )
		{
			try
			{
				this.wait();
			}
			catch ( InterruptedException e )
			{
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Unlocks the GuiLock.
	 * 
	 */
	public synchronized void unlock ()
	{
		locked = false;
		this.notifyAll();
	}
	
	/**
	 * Delays the current thread for the given time.
	 * 
	 * @param time time in milliseconds to lock.
	 */
	public synchronized void delay ( long time )
	{
		try
		{
			this.wait( time );
		}
		catch ( InterruptedException e )
		{
			e.printStackTrace();
		}
	}
}